﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using Uploaders;

namespace Commands.Settings
{
    [DataContract]
    [KnownType(typeof(List<string>))]
    public class Settings
    {
        public Settings()
        {
            MailCredentials = new MailSettings();
            SharingServices = new FileSharingServicesSettings();
            Trackers = new TrackerServiceSettings();
            ProgrammsPaths = new ProgrammsPaths();
            TimeSettings = new TimeSettings();
        }

        [DataMember]
        public string PluginsPath { get; set; }

        [DataMember]
        public MailSettings MailCredentials { get; set; }

        [DataMember]
        public FileSharingServicesSettings SharingServices { get; set; }

        [DataMember]
        public TrackerServiceSettings Trackers { get; set; }

        [DataMember]
        public ProgrammsPaths ProgrammsPaths { get; set; }

        [DataMember]
        public TimeSettings TimeSettings { get; set; }

        [DataMember]
        public string DownloadFolder { get; set; }

        [DataMember]
        public string DefaultPassword { get; set; }

        
        [DataMember]
        public AllowedEmails AllowedEmails { get; set; }

        [DataMember]
        public HiddeSettings HideSettings { get; set; }
    }

    [DataContract]
    public class HiddeSettings
    {

        [DataMember]
        public bool StartMinimized { get; set; }

        [DataMember]
        public bool DontShowTrayIcon { get; set; }
    }

    [CollectionDataContract]
    public class AllowedEmails : List<string>
    {
        public AllowedEmails()
        {
        }
    }

    [DataContract]
    public class ServiceSettings : ISimpleCredentials
    {
        [DataMember]
        public string Password { get; set; }

        [DataMember]
        public string Login { get; set; }
    }

    [DataContract]
    [Serializable]
    public class TrackerServiceSettings
    {
        public TrackerServiceSettings()
        {
            RuTracker = new ServiceSettings();
            FreeTorrents = new ServiceSettings();
        }

        [DataMember]
        public ServiceSettings RuTracker { get; set; }

        [DataMember]
        public ServiceSettings FreeTorrents { get; set; }

        [DataMember]
        public ServiceSettings TapochekNet { get; set; }

        [DataMember]
        public List<Mapping> FoldersMapping { get; set; } 
    }

    [DataContract]
    public class Mapping
    {
        public string Name { get; set; }
        public string Folder { get; set; }
    }

    [Serializable]
    public class SerializableKeyValue<K, V>
    {
        public K Key { get; set; }
        public V Value { get; set; }
    }

    [DataContract()]
    public class ProgrammsPaths
    {
        [DataMember]
        public string TorrentClient { get; set; }

        [DataMember]
        public string Teamviewer { get; set; }
    }

    [DataContract]
    public class TimeSettings
    {
        [DataMember]
        public int MinutesBetweenMailCheck { get; set; }

        [DataMember]
        public int MinutesBetweenSuspend { get; set; }

        [DataMember]
        public bool NeedToSuspend { get; set; }

        [DataMember]
        public bool AutoCheckEnabled { get; set; }

        [DataMember]
        public bool CheckAtStart { get; set; }

        [DataMember]
        public bool SuspendOnlyOnInactive { get; set; }

        [DataMember]
        public List<SerializableTimeSpan> WakeUpTimes { get; set; }

        [DataMember]
        public List<TimeIntervals> CantSuspendTimes { get; set; }

        [DataMember]
        public bool ShouldProgramAutostart { get; set; }
    }

    [DataContract]
    public class TimeIntervals
    {
        [DataMember]
        public SerializableTimeSpan Start { get; set; }

        [DataMember]
        public SerializableTimeSpan Finish { get; set; }

        public bool IsTimeInsideInterval(DateTime time)
        {
            return time.TimeOfDay >= Start && time.TimeOfDay <= Finish;
        }
    }

    [XmlType(TypeName = "WakeupTime")]
    public class SerializableTimeSpan
    {
        public SerializableTimeSpan()
        {

        }

        public SerializableTimeSpan(TimeSpan timeSpan)
        {
            TimeSpan = timeSpan;
        }

        public static implicit operator SerializableTimeSpan(TimeSpan timeSpan)
        {
            return new SerializableTimeSpan(timeSpan);
        }
        public static implicit operator TimeSpan(SerializableTimeSpan serializableTimeSpan)
        {
            return serializableTimeSpan.TimeSpan;
        }

        [XmlIgnore]
        public TimeSpan TimeSpan { get; set; }

        [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
        [XmlText]
        public string TimeSinceLastEventString
        {
            get
            {
                return XmlConvert.ToString(TimeSpan);
            }
            set
            {
                TimeSpan = string.IsNullOrEmpty(value) ?
                    TimeSpan.Zero : XmlConvert.ToTimeSpan(value);
            }
        }

    }
}
